package net.tngou.action.api;

import java.util.HashMap;
import java.util.Map;

import net.tngou.action.BaseAction;
import net.tngou.pojo.User;
import net.tngou.service.UserService;
import net.tngou.util.DigestMD;
import net.tngou.util.DigestSHA;
import net.tngou.util.OAuthUtil;
import net.tngou.util.StringUtil;
import net.tngou.util.ValidateTools;

public class OauthApi extends BaseAction {

	/**
	 * 登录
	 */
	public void login() {

		String username = request.getParameter("username");
	    String password = request.getParameter("password");
		String save_login = request.getParameter("save_login");//是否保存密码
		UserService userService = new UserService();
		User user = userService.getUser(username, DigestMD.MD5(password));
		if(user!=null)
		{
				
				String access_token = OAuthUtil.Access_token(user.getUsername(), user.getPassword()); //
		
				session.setUser(user);
				if(save_login==null)
				{
					response.addAutoLoginCookie(access_token);//临时 Cookie
				}else
				{
					response.addAutoLoginCookie(60*60*24*365, access_token);//保存1年的Cookie
				}
				run_success(access_token);
			}else
			{
				run_false("该用户不能正常登录！");
			}
		

	}
	
	/**
	 * 退出
	 */
	public void exit()
	{	
		String returnUrl = request.getParameter("rurl");
		if(returnUrl==null)
		{
		 returnUrl=getDomain().getBase();
		}
		if(tngou_id==null) 
		{
			sendRedirect(returnUrl);
			return;
		}
		String session_id= request.getParams()!=null?request.getParams()[0]:null;	
		if (tngou_id.equals(session_id))
		{
			
			session.deleteUser(tngou_id);
			response.removeAutoLoginCookie();//溢出cook
		}
		sendRedirect(returnUrl);
		return;
	
	}

	
	
	
}
